﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GMS.BIZ;

namespace Web.SetSystem
{
    public partial class WebForm7 : System.Web.UI.Page
    {
        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            //判断用户权限
            if (!((Account)HttpContext.Current.Session["Account"]).RoleCurrent.Equals('a')) Response.Redirect("Default.aspx");
            // 班级库添加过滤
            if (!IsPostBack || Tbx_filter.Text == string.Empty)
            {
                ArrayList myAL = Library.GetClassLib();
                ArrayList GridViewArray = new ArrayList();
                for (int i = 0; i < myAL.Count; i++)
                {
                    ClassInfo newClass = new ClassInfo(((ListItem)myAL[i]).Text);
                    newClass.ClassTeacher = Staff.GetName(newClass.ClassTeacher);
                    GridViewArray.Add(newClass);
                }

                GridViewClass.DataSource = GridViewArray;
                GridViewClass.DataBind();
            }
            if (Tbx_filter.Text != string.Empty)
            {
                ArrayList myAL = new ArrayList();
                myAL = Library.GetClassLib(Tbx_filter.Text.ToString());
                ArrayList GridViewArray = new ArrayList();
                for (int i = 0; i < myAL.Count; i++)
                {
                    ClassInfo newClass = new ClassInfo(((ListItem)myAL[i]).Text);
                    newClass.ClassTeacher = Staff.GetName(newClass.ClassTeacher);
                    GridViewArray.Add(newClass);
                }

                GridViewClass.DataSource = GridViewArray;
                GridViewClass.DataBind();
            }

            // 班主任归属页面获取系统所有班主任姓名
            if (!IsPostBack)
            {
                DataSet myDs = ClassTeacher.GetAllClassTeacher();
                if (myDs.Tables[0].Rows.Count == 0) DropDownList_classtchname.Items.Add("无");
                else
                {
                    for (int i = 0; i < myDs.Tables[0].Rows.Count; i++)
                    {
                        DropDownList_classtchname.Items.Add(myDs.Tables[0].Rows[i]["tch_name"].ToString());
                    }
                    //获得选中班主任所带班级
                    refreshTchClassInfo();
                }
                //班主任归属页面获取系统所有班级名称
                ArrayList myAL2 = Library.GetClassLib();
                if (myAL2.Count == 0) DropDownList_classes.Items.Add("无");
                else
                    for (int j = 0; j < myAL2.Count; j++)
                    {
                        DropDownList_classes.Items.Add(myAL2[j].ToString());
                    }
                //获取学生页面班级学生信息
                refreshclassdropdownlist();
                refreshClassStuList();            
            }
        }

        protected void refreshClassStuList()
        {
            if (DropDownList_stuclass.SelectedItem.Text!="空")
            {
                ClassInfo myCI = new ClassInfo(DropDownList_stuclass.SelectedItem.Text);
                DataSet myDS = myCI.GetStudentsDataSet();
                GridView_stulist.DataSource = myDS;
                GridView_stulist.DataBind();
            }
        }

        protected void refreshclassdropdownlist()
        {
            //添加学生页面获取系统所有班级名称
            DropDownList_stuclass.Items.Clear();
            ArrayList myAL3 = Library.GetClassLib();
            if (myAL3.Count == 0) DropDownList_stuclass.Items.Add("空");
            else
                for (int k = 0; k < myAL3.Count; k++)
                    DropDownList_stuclass.Items.Add(myAL3[k].ToString());
        }

        protected void ImgBut_addClass_Click(object sender, ImageClickEventArgs e)
        {
            Library myLb = new Library();
            Lbl_result.Text = myLb.AddClass(Tbx_classname.Text.ToString(),null, Tbx_classdes.Text.ToString());
            if (Lbl_result.Text == Resources.ErrorMessage.OK) DropDownList_classes.Items.Add(Tbx_classname.Text);
            refreshclassdropdownlist();
        }

        protected void ImgBut_delClass_Click(object sender, ImageClickEventArgs e)
        {
            Library myLb = new Library();
            Lbl_result.Text = myLb.DeleteClass(Tbx_classname.Text.ToString());
            if (Lbl_result.Text == Resources.ErrorMessage.OK) DropDownList_classes.Items.Remove(Tbx_classname.Text);
            if (DropDownList_classtchname.SelectedItem.Text!="无") refreshTchClassInfo();
            refreshclassdropdownlist();
        }

        protected void ImgBut_updateclass_Click(object sender, ImageClickEventArgs e)
        {
            Library myLb = new Library();
            Lbl_result.Text = myLb.UpdateClass(Tbx_classname.Text.ToString(), string.Empty, Tbx_classdes.Text.ToString());
        }

        protected void GridViewClass_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewClass.PageIndex = e.NewPageIndex;            

        }

        protected void ImgBut_filter_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList myAL;
            if (Tbx_filter.Text == "") myAL = Library.GetClassLib();
            else
                myAL = Library.GetClassLib(Tbx_filter.Text.ToString());
            ArrayList GridViewArray = new ArrayList();
            for (int i = 0; i < myAL.Count; i++)
            {
                ClassInfo newClass = new ClassInfo(((ListItem)myAL[i]).Text);
                newClass.ClassTeacher = Staff.GetName(newClass.ClassTeacher);
                GridViewArray.Add(newClass);
            }

            GridViewClass.DataSource = GridViewArray;
            GridViewClass.DataBind();
        }               

        protected void ImgBut_updatepsw_Click(object sender, ImageClickEventArgs e)
        {
            User myUser = new User();
            myUser.UserID = Tbx_stdidpsw.Text.ToString();
            Lbl_result3.Text = myUser.ChangePassword(Tbx_newpsw.Text.ToString());
        }
        

        protected void ImageButton_addtch_Click(object sender, ImageClickEventArgs e)
        {
            Library myLb = new Library();
            Staff mySt = new Staff();
            mySt.TeacherId = Tbx_tchid.Text.ToString();
            mySt.TeacherName = Tbx_tchname.Text.ToString();
            mySt.IfClassTeacher = CheckBox_ifclasstch.Checked;
            mySt.IfTeacher = CheckBox_iftch.Checked;
            Lbl_result4.Text = myLb.AddStaff(mySt);
            Lbl_result5.Text = Account.Add(Tbx_tchid.Text.ToString(), Tbx_tchid.Text.ToString(), Tbx_tchid.Text.ToString(), CheckBox_ifadmin.Checked, CheckBox_ifclasstch.Checked, CheckBox_iftch.Checked, false);
            if (mySt.IfClassTeacher == true && Lbl_result4.Text == Resources.ErrorMessage.OK && Lbl_result5.Text == Resources.ErrorMessage.OK) DropDownList_classtchname.Items.Add(mySt.TeacherName);

        }

        protected void ImageButton_deltch_Click(object sender, ImageClickEventArgs e)
        {
            Library myLb = new Library();
            Staff mySt = new Staff(Tbx_tchid.Text);
            if (mySt.IfClassTeacher)
            {
                ClassTeacher myCT = new ClassTeacher(mySt.TeacherId);
                int n = myCT.Classes.Count();
                if (n != 0) Lbl_result4.Text = "该班主任带班中,不能删除!";}
                else
                {
                    Lbl_result4.Text = myLb.DeleteStaff(Tbx_tchid.Text.ToString());
                    if (mySt.IfClassTeacher == true && Lbl_result4.Text == Resources.ErrorMessage.OK) DropDownList_classtchname.Items.Remove(mySt.TeacherName);
                    refreshTchClassInfo();
                }            
        }

        protected void ImageButton_updatetch_Click(object sender, ImageClickEventArgs e)
        {
            Library myLb = new Library();
            Staff mySt = new Staff(Tbx_tchid.Text);
            int n=0;
            if (mySt.IfClassTeacher && CheckBox_ifclasstch.Checked==false)
            {
                ClassTeacher myCT = new ClassTeacher(mySt.TeacherId);
                n = myCT.Classes.Count();
                if (n != 0)  mySt.IfClassTeacher = true;
                else mySt.IfClassTeacher = CheckBox_ifclasstch.Checked;
            }                
                    mySt.TeacherId = Tbx_tchid.Text;
                    mySt.TeacherName = Tbx_tchname.Text;                    
                    mySt.IfTeacher = CheckBox_iftch.Checked;
            if (n==0)  Lbl_result4.Text = myLb.UpdateStaff(mySt);
            else Lbl_result4.Text = "该班主任带班中,班主任状态保留!";
            Lbl_result5.Text = Account.Update(Account.GetLoginName(mySt.TeacherId), string.Empty, mySt.TeacherId, CheckBox_ifadmin.Checked, CheckBox_ifclasstch.Checked, CheckBox_iftch.Checked, false);
                       
        }

        protected void ImgBut_edittchpass_Click(object sender, ImageClickEventArgs e)
        {
            User myUser = new User();
            myUser.UserID = Account.GetLoginName(Tbx_tchidpass.Text);
            Lbl_result5.Text = myUser.ChangePassword(Tbx_tchnewpass.Text);
        }

        protected void ImageButton_findtchid_Click(object sender, ImageClickEventArgs e)
        {
            Tbx_tchid.Text = Staff.GetTchId(Tbx_tchname.Text);
            if (Tbx_tchid.Text != Resources.ErrorMessage.NoSuchData)
            {
                string role = Account.GetRoles(Tbx_tchid.Text);
                CheckBox_ifadmin.Checked = role.Contains('a');
                CheckBox_iftch.Checked = role.Contains('t');
                CheckBox_ifclasstch.Checked = role.Contains('c');
            }
            
        }

        protected void ImageButton_addtchclass_Click(object sender, ImageClickEventArgs e)
        {
            ClassTeacher myCT = new ClassTeacher(Staff.GetTchId(DropDownList_classtchname.SelectedItem.Text));
            Lbl_result7.Text = myCT.Classes.Add(DropDownList_classes.SelectedItem.Text);
            refreshTchClassInfo();
        }

        protected void refreshTchClassInfo()
        {
            ClassTeacher myCT = new ClassTeacher(Staff.GetTchId(DropDownList_classtchname.SelectedItem.Text));
            ArrayList myAL = myCT.Classes.GetList();
            GridView_classes.DataSource = myAL;
            GridView_classes.DataBind();
            if (GridView_classes.Rows.Count!=0) GridView_classes.HeaderRow.Cells[0].Text = "当前班主任所带班级";
        }

        protected void DropDownList_classtchname_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshTchClassInfo();
        }

        protected void ImageButton_deltchclass_Click(object sender, ImageClickEventArgs e)
        {
            ClassTeacher myCT = new ClassTeacher(Staff.GetTchId(DropDownList_classtchname.SelectedItem.Text));
            Lbl_result7.Text = myCT.Classes.Delete(DropDownList_classes.SelectedItem.Text);
            refreshTchClassInfo();
        }

        protected void ImageButton_updatetchclass_Click(object sender, ImageClickEventArgs e)
        {
            ClassTeacher myCT = new ClassTeacher(Staff.GetTchId(DropDownList_classtchname.SelectedItem.Text));
            Lbl_result7.Text = myCT.Classes.Update(DropDownList_classes.SelectedItem.Text);
            refreshTchClassInfo();
        }

        protected void ImageButton_Batchaddstu_Click(object sender, ImageClickEventArgs e)
        {
            if (FileUpload1.HasFile)
            {
                string filetype = FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
                if (filetype != "xls") Lbl_result1.Text = "文件类型只能为xls,请重新上传！";
                else
                {
                    DirectoryInfo myDI = new DirectoryInfo(Server.MapPath("\\") + "setsystem\\importfiles");
                    myDI.Create();
                    string filepath = Server.MapPath("importfiles") + "\\" + FileUpload1.FileName;
                    try
                    {
                        FileUpload1.SaveAs(filepath);
                        Lbl_result1.Text = Import.ImportExcel(FileUpload1.FileName);
                    }
                    catch (Exception exp)
                    {
                        Lbl_result1.Text = exp.Message;
                    }
                    refreshclassdropdownlist();
                    refreshClassStuList();
                }
            }
            else Lbl_result1.Text = "请指定上传文件!";
        }

        protected void GridView_stulist_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_stulist.PageIndex = e.NewPageIndex;
            refreshClassStuList();
        }

        protected void DropDownList_stuclass_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshClassStuList();
        }

        protected void GridView_stulist_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            Library myLb = new Library();
            Lbl_result2.Text = myLb.DeleteStudent(((Label)GridView_stulist.Rows[e.RowIndex].FindControl("Label1")).Text);
            refreshClassStuList();
        }

        protected void GridView_stulist_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView_stulist.EditIndex = e.NewEditIndex;
            refreshClassStuList();
        }

        protected void GridView_stulist_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView_stulist.EditIndex = -1;
            refreshClassStuList();
        }

        protected void GridView_stulist_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Library myLb = new Library();
            Student myStudent = new Student(((Label)GridView_stulist.Rows[e.RowIndex].FindControl("Label6")).Text);            
            myStudent.StudentName = ((TextBox)GridView_stulist.Rows[e.RowIndex].FindControl("TextBox2")).Text;
            myStudent.GraduateYear = ((TextBox)GridView_stulist.Rows[e.RowIndex].FindControl("TextBox4")).Text;
            Lbl_result2.Text = myStudent.PersonalInfomationEdit();
            GridView_stulist.EditIndex = -1;
            refreshClassStuList();
        }

        protected void GridView_classes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView_classes.PageIndex = e.NewPageIndex;
            refreshTchClassInfo();
        }
       
    }
}
